package contents;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.sql.SQLException;

import contents.entities.Class;
import contents.entities.Student;
import contents.entities.Teacher;
import contents.entities.Subject;
import contents.entities.DateSubstitutions;
import contents.entities.SubstitutionItem;
import database.Database;
import database.DatabaseEntities;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;

public class ListsContent {
    
    public static HttpServletRequest setClassesListContent(HttpServletRequest request) throws SQLException {
        String classIdString = request.getParameter("classId");

        if (classIdString == null) {
            Map<Integer, Class> classes = Database.selectClasses();
            request.setAttribute("classes", classes.values());
            
        } else {
            int classId = Integer.valueOf(classIdString).intValue();
            
            request.setAttribute("className", Database.selectClassName(classId));
            Map<Integer, Student> students = Database.selectStudentsOfClass(classId);
            request.setAttribute("students", students.values());   
        }
        
        return request;
    }

    public static HttpServletRequest setTeachersListContent(HttpServletRequest request) throws SQLException {
        Map<Integer, Teacher> teachers = Database.selectTeachers();
        request.setAttribute("teachers", teachers.values());
        return request;
    }

    public static HttpServletRequest setSubstitutionsListContent(HttpServletRequest request) throws SQLException {
        Map<Integer, DateSubstitutions> dateSubstitutions = new HashMap<Integer, DateSubstitutions>();
        
        // get next monday date
        Date mondayDate = common.CommonMethods.getMondayDate();

        Date currentDate;
        for (int i = 0; i < 5; i++) {
            currentDate = new Date(mondayDate.getTime());
            currentDate.setDate(currentDate.getDate() + i);

            // get substitutions of the day
            List<DatabaseEntities.Substitution> substitutions = Database.selectSubstitutionsOfDate(currentDate);
            
            List<SubstitutionItem> substitutionItems = new ArrayList<SubstitutionItem>();
            
            for (int j = 0; j < substitutions.size(); j++) {
                DatabaseEntities.Substitution substitution = substitutions.get(j);
                
                String className = Database.selectClassName(substitution.classId);
                Integer lesson = Integer.valueOf(substitution.unit);
                Subject subject = Database.getSubject(substitution.subjectId);
                Teacher missingTeacher = Database.getTeacher(substitution.missingTeacherId);
                
                Teacher substitutingTeacher;
                if (substitution.substitutingTeacherId == null) {
                    substitutingTeacher = null;
                } else {
                    substitutingTeacher = Database.getTeacher(substitution.substitutingTeacherId.intValue());
                }
            
                substitutionItems.add(new SubstitutionItem(className, lesson, subject, missingTeacher, substitutingTeacher));
            }
            Collections.sort(substitutionItems, new SubstitutionItem.ByClassNameAndTimeComparator());
            
            dateSubstitutions.put(i, new DateSubstitutions(currentDate, substitutionItems));
        }
        
        request.setAttribute("substitutions", dateSubstitutions.values());
        return request;
    }
    
    private ListsContent() {
    }
}
